package com.ccjq.jq173.order.dao.impl;

import com.ccjq.jq173.core.util.ArithUtils;
import com.ccjq.jq173.core.util.DbConnection;
import com.ccjq.jq173.order.dao.OrderDao;
import com.ccjq.jq173.order.po.Order;
import com.ccjq.jq173.order.po.OrderProduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public List<Order> queryOrders(String userId, int startRow, int endRow) {
        List<Order> orders = new ArrayList<Order>();
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        String sql = "select * from (select  order_id,create_time,payment,amount,receiver,province,city,county,address,zipcode,telphone,user_id,rownum rn from t_order where user_id=?)t1 where t1.rn between ? and ?";
        //String sql="select * from(select * from (SELECT ORDER_ID,CREATE_TIME,RAYMENT,AMOUNT,RECEIVER,PROVINCE,CITY,COUNTY,ADDRESS,ZIPCODE,TELPHONE,USER_ID,rownum rn FROM t_order WHERE USER_ID=?)t1 where t1.rn>=?)t2 where t2.rn<=?";
        try {
            conn = DbConnection.getConnection();
            st = conn.prepareStatement(sql);
            st.setString(1,userId);
            st.setInt(2, startRow);
            st.setInt(3, endRow);
            rs = st.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getString(1));
                order.setCreateTime(rs.getDate(2));
                order.setPayment(rs.getInt(3));
                order.setAmount(rs.getInt(4));
                order.setReceiver(rs.getString(5));
                order.setProvince(rs.getString(6));
                order.setCity(rs.getString(7));
                order.setCounty(rs.getString(8));
                order.setAddress(rs.getString(9));
                order.setZipcode(rs.getString(10));
                order.setTelphone(rs.getString(11));
                order.setUserId(rs.getString(12));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(rs, st, conn);
        }
        return orders;
    }

    @Override
    public int getCount(String userId) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        int result = 0;
        String sql = "select user_id from t_order where user_id=?";

        try {
            conn = DbConnection.getConnection();
            st = conn.prepareStatement(sql);
            st.setString(1, userId);
            rs = st.executeQuery();
            while (rs.next()) {
                result++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Order getOrderDetail(String orderId) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        Order o = null;
        String sql="select order_id,create_time,payment,amount,receiver,province,city,county,address,zipcode,telphone,user_id from t_order where order_id=?";

        try {
            conn=DbConnection.getConnection();
            st=conn.prepareStatement(sql);
            st.setString(1,orderId);
            rs=st.executeQuery();
            if(rs.next())
            {
                o=new Order();
                o.setOrderId(rs.getString(1));
                o.setCreateTime(rs.getDate(2));
                o.setPayment(rs.getInt(3));
                o.setAmount(rs.getDouble(4));
                o.setReceiver(rs.getString(5));
                o.setProvince(rs.getString(6));
                o.setCity(rs.getString(7));
                o.setCounty(rs.getString(8));
                o.setAddress(rs.getString(9));
                o.setZipcode(rs.getString(10));
                o.setTelphone(rs.getString(11));
                o.setUserId(rs.getString(12));
                o.setOrderps(l(orderId));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(rs,st,conn);
        }
        return o;
    }
    public List<OrderProduct> l(String orderId)
    {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        ArrayList<OrderProduct> a=new ArrayList<OrderProduct>();
        String sql="select a.product_id,feature,quantity,a.price,order_id from t_order_product a join t_product b on a.product_id=b.product_id where order_id=?";

        try {
            conn=DbConnection.getConnection();
            st=conn.prepareStatement(sql);
            st.setString(1,orderId);
            rs=st.executeQuery();
            while(rs.next()){
                OrderProduct o=new OrderProduct();
                o.setProductId(rs.getString(1));
                o.setFeature(rs.getString(2));
                o.setQuantity(rs.getInt(3));
                o.setPrice(rs.getDouble(4));
                int k=rs.getInt(3);
                double d=rs.getDouble(4);
                double xj= ArithUtils.mul(Double.valueOf(Integer.valueOf(k)),d);
                o.setSubtotal(xj);
                o.setOrderId(rs.getString(5));
                a.add(o);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DbConnection.close(rs,st,conn);
        }
        return a;
    }
    public static void main(String[] args) {
        OrderDaoImpl o = new OrderDaoImpl();
        Order order = o.getOrderDetail("ORD8952DD72");
        System.out.println(order.getOrderps().size());
    }
}
